//
//  ZYNewFeatureViewController.swift
//  weibo007
//
//  Created by 陈宗毅 on 15/12/17.
//  Copyright © 2015年 czy. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class ZYNewFeatureViewController: UICollectionViewController {
    
    private let layout = UICollectionViewFlowLayout();
    
    init(){
        //调用父类的需要layout的构造函数
        super.init(collectionViewLayout:layout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private let ItemCount = 4
    
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareCollectionView()
        
    }
    
    private func prepareCollectionView(){
        //注册cell
        self.collectionView?.registerClass(ZYNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        //设置布局参数
        layout.itemSize = UIScreen.mainScreen().bounds.size
        //不要间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        //水平滚动
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //分页
        collectionView?.pagingEnabled = true
        //取消弹簧效果
        collectionView?.bounces = false
    }
    
    
    
    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return ItemCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        // 去缓存池中查找
        // as 类型转换, 但是有可能转成功,也有可能转失败
        // as! 表示一定能转成功,如果一旦转换失败,程序崩溃
        // as? 表示有可能转成功,也有可能转不成功, 返回值是一个可选
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ZYNewFeatureCell
        cell.index = indexPath.item
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
       let visibleIndexPath = collectionView.indexPathsForVisibleItems().last!
        //判断是否是最后一个
        if visibleIndexPath.item == ItemCount - 1 {
            let cell = collectionView.cellForItemAtIndexPath(visibleIndexPath)as! ZYNewFeatureCell
            //开始动画
            cell.startButtonAnimation()
        }
        
    }
    
    
    class ZYNewFeatureCell: UICollectionViewCell {
        
        var index = 0 {
            
            didSet {
                bkgImageView.image = UIImage(named: "new_feature_\(index + 1)");
                startButton.hidden = true
            }
        }
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            
            prepareUI()
        }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        //开始按钮动画
        func startButtonAnimation(){
            //显示按钮
            startButton.hidden  = false
            //设置开始按钮的scale
            startButton.transform = CGAffineTransformMakeScale(0, 0)
            //使用UIView动画
            UIView.animateWithDuration(0.8, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
                
                //清空transform
                self.startButton.transform = CGAffineTransformIdentity
                }) { (_) -> Void in
                    
            }
        
        }
        
        private func prepareUI(){
            //添加子控件
            contentView.addSubview(bkgImageView)
            contentView.addSubview(startButton)
            //添加约束
//            bkgImageView.translatesAutoresizingMaskIntoConstraints = false
//            startButton.translatesAutoresizingMaskIntoConstraints = false
            //背景填充父控件
            bkgImageView.ff_Fill(contentView)
//            contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
//            
//            contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
//            
//            contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
//            
//            contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
            // 开始体验按钮约束
            // 水平CenterX和父控件的CenterX重合
//            contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//             // 垂直开始按钮的底部和父控件的底部重合
//            contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
            
            startButton.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint(x: 0, y: -160))
            
        }
        
        
        
        func startButtonClick(){
            

            AppDelegate.outSwitchRootViewController(true)
//            
//                    (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootViewController(true)
        }
        
        
        //MARK: - 懒加载，按钮会自动适配图片的大小
        //背景图片
        private lazy var bkgImageView:UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
        //开始按钮
        private lazy var startButton:UIButton = {
            let button = UIButton()
            //设置背景图片
            button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
            button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
            //设置按钮文字
            button.setTitle("开始体验", forState: UIControlState.Normal)

            button.addTarget(self, action: "startButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
            return button
            
            }()
        
    }

    
    
}
